/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yutel.gui;

import com.yutel.biz.Charge;
import com.yutel.main.WSClientMIDlet;
import com.yutel.util.LangUtil;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author yuanyajun
 */
public class ChargeForm extends Form implements CommandListener, Runnable {

    private TextField cardno;
    private TextField cardpwd;
    private boolean isConfirm = false;
    private Command back;
    private Command submit;
    private Command confirm;
    private Thread t;

    public ChargeForm() {
        super(LangUtil.getInstance().getMessage(LangUtil.tit_register));
        try {
            exeute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void exeute() throws Exception {
        // Set up this Displayable to listen to command events
        cardno = new TextField(LangUtil.getInstance().getMessage(LangUtil.tag_cardno), "", 15, TextField.ANY);
        cardpwd = new TextField(LangUtil.getInstance().getMessage(LangUtil.tag_cardpwd), "", 15, TextField.ANY);
        this.append(cardno);
        this.append(cardpwd);
        setCommandListener(this);
        // add the Exit command
        back = new Command(LangUtil.getInstance().getMessage(LangUtil.but_back), Command.BACK, 1);
        submit = new Command(LangUtil.getInstance().getMessage(LangUtil.but_submit), Command.OK, 1);
        confirm = new Command(LangUtil.getInstance().getMessage(LangUtil.but_confirm), Command.OK, 1);
        addCommand(back);
        addCommand(submit);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getCommandType() == Command.BACK) {
            IndexForm index = new IndexForm();
            WSClientMIDlet.setDisplay(index);
        } else if (command.getCommandType() == Command.OK) {
            t = new Thread(this);
            t.start();
        }
    }

    public void run() {
        Charge charge = new Charge();
        String result = "";
        if (isConfirm) {
            result = charge.confirm(cardno.getString(), cardpwd.getString());
            removeCommand(confirm);
            StringItem resultItem = new StringItem(LangUtil.getInstance().getMessage(LangUtil.str_charge), result);
            this.append(resultItem);
        } else {
            result = charge.query(cardno.getString(), cardpwd.getString());
            boolean re = true;
            if (re) {
                Alert alert = new Alert(LangUtil.getInstance().getMessage(LangUtil.str_alert),
                        LangUtil.getInstance().getMessage(LangUtil.str_alertText),
                        null, AlertType.CONFIRMATION);
                alert.setTimeout(Alert.FOREVER);
                WSClientMIDlet.setDisplay(alert);
                removeCommand(submit);
                addCommand(confirm);
                isConfirm = true;
                StringItem resultItem = new StringItem(LangUtil.getInstance().getMessage(LangUtil.str_preCharge), result);
                this.append(resultItem);
            } else {
                IndexForm index = new IndexForm();
                WSClientMIDlet.setDisplay(index);
                StringItem resultItem = new StringItem(LangUtil.getInstance().getMessage(LangUtil.str_preCharge), "false");
                this.append(resultItem);
            }
        }
    }
}
